const app = getApp()
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    datas: [
      { classa: 'shujyi', classb: 'shuj', bind: 'dianji', a: 1, money:'5元'},
      { classa: 'shujyi', classb: 'shuj', bind: 'dianji', a: 1, money: '10元' },

      { classa: 'shujyi', classb: 'shuj', bind: 'dianji', a: 1, money: '20元' },
      { classa: 'shujyi', classb: 'shuj', bind: 'dianji', a: 1, money: '30元' },

      { classa: 'shujyi', classb: 'shuj', bind: 'dianji', a: 1, money: '50元' },
      { classa: 'shujyi', classb: 'shuj', bind: 'dianji', a: 1, money: '100元' },
    ],
    jine:'',
    getWxUrl: app.data.url + '/api/wxpay/getchongzhi',
    getzhifu: app.data.url + '/api/wxpay/zhifu',
    shuju:[],
    indexs:0,
    congzhijine:'',
    yue:0,
    kongzhi:true,
  },


  //点击金额
  dianji:function(e){
    var index=e.currentTarget.dataset.index;
    var shuju = this.data.shuju;
    this.setData({ indexs: index, congzhijine: shuju[index].dir});
    
  },
  xiugai:function(e){
console.log(e);
  },


  // inoput框监听输入
  watchPassWord: function (event) {
    var congzhijine=event.detail.value;
    var zhen = /^0/;
    var zhenze = /[0123456789.]/;
    if (zhenze.test(congzhijine)){
      congzhijine=congzhijine.replace(/^0/,'');
    }else{
      congzhijine = '';
      wx.showToast({
        title: '请输入数字',
        icon:'none'
      })
    }
    this.setData({ congzhijine: congzhijine})
    
  },
  chongzhi:function(){
    var money = this.data.congzhijine;
    var kongzhi = this.data.kongzhi;
    if(!money){
      wx.showToast({
        title: '请输入充值金额',
        icon:'none'
      })
      return;
    }
    if(!kongzhi){
      return;
    }
    var that = this;
    var user = wx.getStorageSync('user');
    that.setData({kongzhi:false});
    wx.request({
      url: that.data.getzhifu,
      data: { token: user.token, money: money},
      method:'post',
      success:function(res){
        var data = res.data;
        console.log(data);
        // return;
        if(data.code==200){
          that.setData({ kongzhi: true });
          var qianming = data.data;
          console.log(qianming);
          wx.requestPayment({
            timeStamp: qianming.timeStamp,
            nonceStr: qianming.nonceStr,
            package: qianming.package,
            signType: qianming.signType,
            paySign: qianming.paySign,
            success:function(res){
              wx.showToast({
                title: '支付成功',
              })
            },fail:function(res){
              console.log(res);
            }
          })
        }else{
          that.setData({ kongzhi: true });
          wx.showToast({
            title: data.msg,
            icon:'none'
          })
        }
      }
    })
  },


  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var user = wx.getStorageSync('user');
    wx.request({
      url: that.data.getWxUrl,
      data: { token: user.token },
      success:function(res){
        var data = res.data;
        // console.log(data);
        if(data.code == 200){
          var shuju = data.data.data;
          var user = data.data.user;
        
          // console.log(user);
          that.setData({
            shuju:shuju,
            yue: user.balance
          })
        }else{
          wx.showToast({
            title: '系统错误',
            icon: 'none'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var user = wx.getStorageSync('user');
    var titles = wx.getStorageSync('titles');
    return {
      title: titles,
      path: '/pages/index/index?scene' + user.id,
    };
  }
})